package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*不使用批处理操作，向t_batch中插入一万条记录，并记录耗时*/
/*插入了10000条记录
  耗时：12502ms*/
public class JDBCTest17 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DbUtils.getConnection();
            String sql = "insert into t_batch(id, name) values(?, ?)";
            ps = conn.prepareStatement(sql);
            int count = 0;
            for (int i = 1; i <= 10000; i ++) {
                ps.setLong(1, i);
                ps.setString(2, "batch" + i);
                count += ps.executeUpdate();
            }
            System.out.println("插入了" + count + "条记录");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, null);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) + "ms");
    }
}
